package com.gz.tank;

import java.io.*;
import java.util.Vector;

public class Recorder {
    public static int getEnemyNum() {
        return enemyNum;
    }

    public static String recordfilepath = "E:\\Java\\java_code\\java_test\\Test_tank\\gamerecord.txt";

    private static BufferedWriter bw = null;//写
    private static BufferedReader br = null;//读

    private static Vector<EnemyTank> enemyTanks = null;//用于存

    private static Vector<Node> nodes = new Vector<>();//用于读，注意！！不能置为null,否则空指针调用add方法会报错


    public static void setNodes(Vector<Node> nodes) {
        Recorder.nodes = nodes;
    }

    public static String getRecordfilepath() {
        return recordfilepath;
    }

    public static Vector<Node> getNodes() {
        return nodes;
    }

    //读取时获取敌人坐标方法
    public static Vector<Node> getNodesenemy() {
        try {
            br = new BufferedReader(new FileReader(recordfilepath));
            enemyNum = Integer.parseInt(br.readLine());
            String readline = "";
            while ((readline = br.readLine()) != null) {
                String[] str = readline.split(" ");
                nodes.add(new Node(Integer.parseInt(str[0]),
                        Integer.parseInt(str[1]),
                        Integer.parseInt(str[2])));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return nodes;
    }

    public static Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    //存取时存取敌人坐标方法
    public static void keepRecorder() {
        try {
            bw = new BufferedWriter(new FileWriter(recordfilepath));
            bw.write(enemyNum + "");
            bw.newLine();
            for (int i = 0; i < enemyTanks.size(); i++) {
                EnemyTank enemyTank = enemyTanks.get(i);
                //保存敌人的存活坦克坐标
                bw.write(enemyTank.getX() + " "
                        + enemyTank.getY() + " "
                        + enemyTank.getDirect());

                bw.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static void addenemyNum() {
        enemyNum++;
    }

    public static void setEnemyNum(int enemyNum) {
        Recorder.enemyNum = enemyNum;
    }

    private static int enemyNum = 0;
}
